<canvas width=200 height=200 style="outline:1px solid gray;">
    非常抱歉，您的浏览器不支持canvas!
</canvas>

<!-- 顶点着色器 -->
<script type='x-shader/x-vertex' id='vs'>
    attribute vec4 a_position;
    void main(){
        gl_Position = a_position;
    }
</script>

<!-- 片段着色器 -->
<script type='x-shader/x-fragment' id='fs'>
    void main(){
        gl_FragColor=vec4(1.0,0.0,0.0,1.0);
    }
</script>

<script>

    setTimeout(function () {

        var gl = document.getElementsByTagName('canvas')[0].getContext('webgl');

        var loadShader = function (type, source) {
            var shader = gl.createShader(type);
            gl.shaderSource(shader, source);
            gl.compileShader(shader);
            return shader;
        };

        var vertexShader = loadShader(gl.VERTEX_SHADER, document.getElementById('vs').innerHTML),
            fragmentShader = loadShader(gl.FRAGMENT_SHADER, document.getElementById('fs').innerHTML);

        var glProgram = gl.createProgram();

        gl.attachShader(glProgram, vertexShader);
        gl.attachShader(glProgram, fragmentShader);

        gl.linkProgram(glProgram);
        gl.useProgram(glProgram);

        // 数据
        var data = new Float32Array([
            // 顶点坐标3
            -1.0, 1.0, 0,
            -1.0, -1.0, 0,
            1.0, 1.0, 0,
            1.0, -1.0, 0,
        ]);

        var buffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
        gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);

        var location = gl.getAttribLocation(glProgram, 'a_position');
        gl.vertexAttribPointer(location, 3, gl.FLOAT, false, 3 * data.BYTES_PER_ELEMENT, 0);
        gl.enableVertexAttribArray(location);


        // 绘制
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
    });


</script>